package net.zoneland.knowledge.dao;

import java.time.LocalDateTime;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import net.zoneland.knowledge.model.PerformanceMonitor;

/**
 * 性能监控mapper.
 * @author bcc
 */
@Mapper
public interface PerformanceMonitorMapper {

    @Insert({
            "insert into aitk_performance_monitor (ID, request_uri, ",
            "user_id, ip, method, ",
            "terminal_type, cost, ",
            "request_body, request_param, ",
            "response, error_info, create_time)",
            "values (#{id,jdbcType=VARCHAR}, #{requestUri,jdbcType=VARCHAR}, ",
            "#{userId,jdbcType=VARCHAR}, #{ip,jdbcType=VARCHAR}, #{method,jdbcType=VARCHAR}, #{terminalType,jdbcType=VARCHAR}, ",
            "#{cost,jdbcType=VARCHAR}, #{requestBody,jdbcType=VARCHAR}, ",
            "#{requestParam,jdbcType=VARCHAR}, #{response,jdbcType=VARCHAR}, ",
            "#{errorInfo,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP})"
    })
    int insert(PerformanceMonitor record);

    @Update({
            "insert into aitk_performance_monitor_his ",
            "(ID, request_uri, ",
            "user_id, ip, method, ",
            "terminal_type, cost, ",
            "request_body, request_param, ",
            "response, error_info, create_time)",
            "select  ID, request_uri, ",
            "user_id, ip, method, ",
            "terminal_type, cost, ",
            "request_body, request_param, ",
            "response, error_info, create_time ",
            "from aitk_performance_monitor ",
            "where create_time < #{localDateTimeForOp,jdbcType=TIMESTAMP}"
    })
    void syncPerformanceMonitorData(@Param("localDateTimeForOp") LocalDateTime localDateTimeForOp);

    @Delete({
            "delete from aitk_performance_monitor where create_time < #{localDateTimeForOp,jdbcType=TIMESTAMP} "
    })
    void deleteDateByTime(@Param("localDateTimeForOp") LocalDateTime localDateTimeForOp);
}
